package mysql

import "gorm.io/gorm"

// 订单表
type Order struct {
	gorm.Model
	OrderSn string  `gorm:"column:order_sn;type:varchar(50);not null;unique;comment:订单编号"`
	UserId  uint    `gorm:"column:user_id;type:int(11);not null;comment:用户ID"`
	Amount  float32 `gorm:"column:amount;type:decimal(10,2);not null;comment:订单金额"`
	Status  int8    `gorm:"column:status;type:tinyint(1);not null;default:0;comment:订单状态：0未支付，1 已支付，2支付失败"`
	Payment int8    `gorm:"column:payment;type:tinyint(1);not null;comment:支付方式:1支付宝，2微信"`
}

// 订单商品表
type OrderItem struct {
	gorm.Model
	OrderSn    string  `gorm:"column:order_sn;type:varchar(50);not null;unique;comment:订单编号"`
	OrderId    uint    `gorm:"column:order_id;type:int(11);not null;index;comment:订单ID"`
	GoodsId    uint    `gorm:"column:goods_id;type:int(11);not null;comment:商品ID"`
	GoodsTitle string  `gorm:"column:goods_title;type:varchar(100);not null;comment:商品名称"`
	GoodsCover string  `gorm:"column:goods_cover;type:varchar(255);not null;comment:商品图片"`
	Count      int64   `gorm:"column:count;type:int(11);not null;comment:购买数量"`
	GoodsPrice float32 `gorm:"column:goods_price;type:decimal(10,2);not null;comment:购买价格"`
	Status     int8    `gorm:"column:status;type:tinyint(4);not null;default:0;comment:订单状态"`
}

// 创建订单
func (o *Order) Create() error {
	return MysqlClient.Create(&o).Error
}

// 创建订单商品
func (oi *OrderItem) Create() error {
	return MysqlClient.Create(&oi).Error
}

// 查询订单
func (o *Order) GetOrderList() (order Order, err error) {
	err = MysqlClient.Where("order_sn = ?", o.OrderSn).First(&order).Error
	return
}

// 查询订单商品
func (oi *OrderItem) GetOrderItemList() (orderItem OrderItem, err error) {
	err = MysqlClient.Where("order_sn = ?", oi.OrderSn).First(&orderItem).Error
	return
}
